package thread;

import cn.hutool.core.text.StrFormatter;

/**
 * @Author: 582895699@qq.com
 * @Desctription: volatile关键字
 * @Date: Created in 2021/12/14 23:52
 */
public class VolatileTest {

    private volatile static int INIT_VALUE = 0;


    private final static int MAX_VALUE = 5;

    public static void main(String[] args) {
        new Thread(() -> {
            int localValue = INIT_VALUE;
            while (localValue < MAX_VALUE) {
                if (localValue != INIT_VALUE) {
                    System.out.println(StrFormatter.format("value is to [{}]", INIT_VALUE));
                    localValue = INIT_VALUE;
                }
            }
        }).start();

        new Thread(() -> {
            int localValue = INIT_VALUE;
            while (INIT_VALUE < MAX_VALUE) {
                System.out.println(StrFormatter.format("value is udpate to [{}]", ++localValue));
                INIT_VALUE = localValue;

                try {
                    Thread.sleep(500);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        }).start();
    }

}
